package com.beiing.tianshuai.tianshuai.mine.ui;

import android.content.Context;
import android.content.Intent;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.beiing.tianshuai.tianshuai.R;
import com.beiing.tianshuai.tianshuai.app.HttpRequestConstant;
import com.beiing.tianshuai.tianshuai.base.BaseActivity;
import com.beiing.tianshuai.tianshuai.entity.IdentificationInfoBean;
import com.beiing.tianshuai.tianshuai.entity.UserInfoBean;
import com.beiing.tianshuai.tianshuai.fragment.ImagePreviewFragment;
import com.beiing.tianshuai.tianshuai.mine.presenter.IdentificationInfoPresenter;
import com.beiing.tianshuai.tianshuai.mine.view.IdentificationInfoViewImpl;
import com.beiing.tianshuai.tianshuai.widget.CircleImageView;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import cn.bingoogolapple.bgabanner.BGAOnNoDoubleClickListener;

public class IdentificationInfoActivity extends BaseActivity implements IdentificationInfoViewImpl{

    @BindView(R.id.rl_organization_tel)
    RelativeLayout mRlOrganizationTel;
    @BindView(R.id.rl_organization_name)
    RelativeLayout mRlOrganizationName;
    @BindView(R.id.toolbar_iv_back)
    ImageView mToolbarIvBack;
    @BindView(R.id.toolbar_tv_title)
    TextView mToolbarTvTitle;
    @BindView(R.id.label_1)
    TextView mLabel1;
    @BindView(R.id.et_unit_name)
    TextView mEtUnitName;
    @BindView(R.id.et_user_name)
    TextView mEtUserName;
    @BindView(R.id.et_user_tel)
    TextView mEtUserTel;
    @BindView(R.id.btn_view_id_card)
    CircleImageView mBtnViewIdCard;
    @BindView(R.id.btn_view_certificate)
    CircleImageView mBtnViewCertificate;
    @BindView(R.id.fragment_container)
    FrameLayout mFragmentContainer;

    private boolean                         isImgFragmentShow;
    private Unbinder                        mUnbinder;
    private Context                         mContext;
    private ImagePreviewFragment            mIdCardPreviewFragment;
    private ImagePreviewFragment            mCertificatePreviewFragment;
    private IdentificationInfoPresenter     mPresenter;
    private String                          mType;
    private ArrayList<String>               mCardImageUrls = new ArrayList<>();
    private ArrayList<String>               mCertificateUrls = new ArrayList<>();


    @Override
    protected int getLayoutId() {
        return R.layout.activity_identification_info;
    }

    @Override
    protected void init() {
        mUnbinder = ButterKnife.bind(this);
        mContext = this;
        initToolbar();
        initListener();
        initPresenter();
    }

    // 初始化toolbar
    private void initToolbar() {
        mToolbarTvTitle.setText("我的认证");
        mToolbarIvBack.setVisibility(View.VISIBLE);
    }

    // 初始化点击事件
    private void initListener() {
        // 返回按钮
        mToolbarIvBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        // 查看身份证信息
        mBtnViewIdCard.setOnClickListener(new BGAOnNoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                isImgFragmentShow = true;
                if (mCardImageUrls != null && mCardImageUrls.size() > 0) {
                    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                    transaction.setCustomAnimations(R.anim.fade_in, R.anim.fade_out);
                    if (mIdCardPreviewFragment == null) {
                        mIdCardPreviewFragment = ImagePreviewFragment.newInstance(mCardImageUrls);
                        transaction.add(R.id.fragment_container, mIdCardPreviewFragment);
                    } else {
                        transaction.show(mIdCardPreviewFragment);
                    }
                    transaction.commit();
                    setStatusBarVisibility(true);
                } else {
                    showShortText("证件图片获取失败");
                }
            }
        });

        // 查看证书信息
        mBtnViewCertificate.setOnClickListener(new BGAOnNoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                isImgFragmentShow = true;
                if (mCertificateUrls != null && mCertificateUrls.size() > 0) {
                    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                    transaction.setCustomAnimations(R.anim.fade_in, R.anim.fade_out);
                    if (mCertificatePreviewFragment == null) {
                        mCertificatePreviewFragment = ImagePreviewFragment.newInstance(mCertificateUrls);
                        transaction.add(R.id.fragment_container, mCertificatePreviewFragment);
                    } else {
                        transaction.show(mCertificatePreviewFragment);
                    }
                    transaction.commit();
                    setStatusBarVisibility(true);
                } else {
                    showShortText("证件图片获取失败");
                }
            }
        });
    }

    // type = personal,company,corporation
    private void initPresenter() {
        Intent intent = getIntent();
        mType = intent.getStringExtra("type");
        switch (mType) {
            case "2":
                mType = "personal";
                mRlOrganizationName.setVisibility(View.GONE);
                mRlOrganizationTel.setVisibility(View.GONE);
                mBtnViewIdCard.setVisibility(View.GONE);
                break;
            case "3":
                mType = "corporation";
                mLabel1.setText("社团名称");
                mRlOrganizationName.setVisibility(View.VISIBLE);
                mRlOrganizationTel.setVisibility(View.VISIBLE);
                break;
            case "4":
                mType = "company";
                mLabel1.setText("企业名称");
                mRlOrganizationName.setVisibility(View.VISIBLE);
                mRlOrganizationTel.setVisibility(View.VISIBLE);
                break;
        }
        mPresenter = new IdentificationInfoPresenter(this);
        mPresenter.getIdentificationInfo(UserInfoBean.getUid(mContext), mType);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mUnbinder != null) {
            mUnbinder.unbind();
        }
    }

    /**
     * 显示隐藏状态栏，全屏不变，只在有全屏时有效
     *
     * @param enable 是否全屏
     */
    private void setStatusBarVisibility(boolean enable) {
        if (enable) {
            getWindow().setFlags(
                    WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN
            );
        } else {
            getWindow().clearFlags(
                    WindowManager.LayoutParams.FLAG_FULLSCREEN
            );
        }
    }

    public void hidePreviewIdCardFragment() {
        isImgFragmentShow = false;
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.setCustomAnimations(R.anim.fade_in, R.anim.fade_out);
        if (mIdCardPreviewFragment != null) {
            transaction.hide(mIdCardPreviewFragment);
        }
        if (mCertificatePreviewFragment != null) {
            transaction.hide(mCertificatePreviewFragment);
        }
        transaction.commit();
        setStatusBarVisibility(false);
    }

    @Override
    public void showProgress() {

    }

    @Override
    public void hideProgress() {

    }

    @Override
    public void onRequestSuccess(IdentificationInfoBean bean) {
        IdentificationInfoBean.DataBean.NumberBean infoBean = bean.getData().getNumber();
        if (infoBean != null) {
            String name = infoBean.getName();
            String tel = infoBean.getTel();
            String cardJust = infoBean.getCardJust();
            String cardBack = infoBean.getCardBack();

            mEtUserName.setText(name);
            mEtUserTel.setText(tel);
            if ("personal".equals(mType)) {
                mCertificateUrls.add(HttpRequestConstant.IDENTIFICATION_PIC_HEAD + cardJust);
            } else if ("corporation".equals(mType)){
                mCardImageUrls.add(HttpRequestConstant.IDENTIFICATION_PIC_HEAD + cardJust);
                mCardImageUrls.add(HttpRequestConstant.IDENTIFICATION_PIC_HEAD + cardBack);
                mCertificateUrls.add(HttpRequestConstant.IDENTIFICATION_PIC_HEAD + infoBean.getCorporation());
                mEtUnitName.setText(infoBean.getTitle());
            } else if ("company".equals(mType)) {
                mCardImageUrls.add(HttpRequestConstant.IDENTIFICATION_PIC_HEAD + cardJust);
                mCardImageUrls.add(HttpRequestConstant.IDENTIFICATION_PIC_HEAD + cardBack);
                mCertificateUrls.add(HttpRequestConstant.IDENTIFICATION_PIC_HEAD + infoBean.getCompany());
                mEtUnitName.setText(infoBean.getTitle());
            }
        }
    }

    @Override
    public void onRequestFailed(Throwable throwable) {
        showShortText("网络异常");
    }

    @Override
    public void onBackPressed() {
        if (isImgFragmentShow) {
            hidePreviewIdCardFragment();
        } else {
            finish();
        }
    }
}
